package u1project.server;

import u1project.common.Request;
import u1project.common.Response;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author oldliu
 * @since 1.0
 */
public class MainServer {
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(20);
        try {
            ServerSocket st = new ServerSocket(9998);
            while (true) {
                es.execute(new ServerThread(st.accept()));
                //  new Thread(new ServerThread(st.accept())).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class ServerThread implements Runnable {
    private final Socket s;

    public ServerThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        System.out.println(s + "连接了");
        try {
            InputStream inputStream = s.getInputStream();
            OutputStream outputStream = s.getOutputStream();
            ObjectInputStream oin = new ObjectInputStream(inputStream);
            ObjectOutputStream oout = new ObjectOutputStream(outputStream);
            Request req = null;
            Response resp = null;
            while (true) {
                req = (Request) oin.readObject();
                //处理请求
                Method method = this.getClass().getDeclaredMethod(req.getAction(),
                        Request.class);
                resp = (Response) method.invoke(this, req);


//                if (req.getAction().equals("login")) {
//                    response=login(req);
//                } else if (req.getAction().equals("register")) {
//                    response=register(req);
//                } else {
//                    //功能没有实现
//                    response = new Response(false, "功能未实现");
//                }
                oout.writeObject(resp);
                oout.flush();
                oout.reset();//防止重发上次的对象
            }
        } catch (Exception e) {
            //e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {
            }
        }
    }

    public Response login(Request request) {
        Response response = null;
        String name = (String) request.get("username");
        String pass = (String) request.get("userpass");
        if ("laoliu".equals(name) && "1234".equals(pass)) {
            response = new Response(true);
        } else {
            response = new Response(false, "错误的而用户名或者密码！");
        }
        return response;
    }

    public Response register(Request request) {
        Response response = null;
        String name = (String) request.get("username");
        System.out.println(name + "准备注册了。。。");
        String pass = (String) request.get("userpass");
        if ("laoliu".equals(name)) {
            response = new Response(false, "用户名已经存在");
        } else if (pass.length() < 4) {
            response = new Response(false, "密码长度太短");
        } else {
            response = new Response(true, "注册");
        }
        return response;
    }

}

